package com.zu.commu.insertDataServletXml.localOperate;

import com.zu.commu.insertDataServletXml.PostDataRemote;

public class SaveRemote
{
  private static boolean canRemoteSave = false;

  private static String servletUrl = null;

  private static Object synObj = new Object();

  private static SaveRemote instance = null;

  private static SaveRemoteThread srThread = null;
  private static String year;
  private static String month;
  private static String date;
  private static String hour;
  private static String minute;
  private static String second;

  public static SaveRemote instance()
  {
    if (instance == null) {
      instance = new SaveRemote();
      srThread = new SaveRemoteThread();
      srThread.start();
    }
    return instance;
  }

  public void setCanRemoteSave(String servletUrl, String year, String month, String date, String hour, String minute, String second)
  {
    synchronized (synObj) {
      canRemoteSave = true;
      this.year = year;
      this.month = month;
      this.date = date;
      this.hour = hour;
      this.minute = minute;
      this.second = second;
      this.servletUrl = servletUrl;
      synObj.notifyAll();
    }
  }

  public void setCannotRemoteSave()
  {
    canRemoteSave = false;
  }

  private static class SaveRemoteThread extends Thread
  {
    public void run()
    {
      while (true)
      {
        if (!SaveRemote.canRemoteSave) {
          synchronized (SaveRemote.synObj) {
            try {
              SaveRemote.synObj.wait();
            }
            catch (Exception localException)
            {
            }
          }
        }
        boolean flag = true;
        String[] xmls = (String[])null;
        while (SaveRemote.canRemoteSave) {
          xmls = XmlOperator.instance().getXmlData(SaveRemote.year, SaveRemote.month, SaveRemote.date, SaveRemote.hour, SaveRemote.minute, SaveRemote.second);
          if (xmls != null)
          {
            for (int i = 0; i < xmls.length; i++) {
              if (xmls[i] != null) {
                flag = new PostDataRemote().post(xmls[i], SaveRemote.servletUrl);
              }
            }
            if (flag)
              continue;
            SaveRemote.canRemoteSave = false;
          }
          else
          {
             SaveRemote.canRemoteSave = false;
          }
        }
         SaveRemote.canRemoteSave = false;
      }
    }
  }
}
